import { createRouter, createWebHistory } from 'vue-router'

// 导入组件
import Login from '@/views/login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import Detail from '@/views/detail/index.vue'
import CartList from '@/views/CartList/index.vue'
import Checkout from '@/views/Checkout/index.vue'
import Pay from '@/views/Pay/index.vue'
import Member from '@/views/Member/index.vue'
import MemberInfo from '@/views/Member/components/UserInfo.vue'
import MemberOrder from '@/views/Member/components/UserOrder.vue'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: Layout,
      // 二级路由
      children: [
        {
          // (小技巧) 滞空默认,当访问一级路由的页面时,就会把Home页面(二级路由)也渲染
          // 默认的二级路由
          path: '',
          component: Home
        },
        {
          path: 'Category/:id',
          component: Category
        },
        {
          path: 'category/sub/:id',
          component: SubCategory
        },
        {
          path: 'detail/:id',
          component: Detail
        },
        {
          path: 'cartlist',
          component: CartList
        },
        {
          path: 'checkout',
          component: Checkout
        },
        {
          path: 'pay',
          component: Pay
        },
        {
          path: 'member',
          component: Member,
          // 三级路由
          children: [
            {
              path: '',
              component: MemberInfo
            },
            {
              path: 'order',
              component: MemberOrder
            }
          ]
        }
      ]
    },
    {
      path: '/Login',
      component: Login
    }
  ],
  // 路由滚动行为定制
  scrollBehavior() {
    // 设置从二级路由切换到一级路由时，将滚动条重置到初始区域
    return { top: 0 }
  }
})

export default router
